package cc

import (
	"learn/pkg/config"
	"reflect"
	"time"
)

// BaseAPIController 基础控制器
type BaseController struct {
}

type BaseWherePage struct {
	Page     int `json:"page"  form:"page" default:"0"`
	PageSize int `json:"page_size"  form:"page_size" default:"20" `
}

func (o *BaseController) ToMap(r interface{}) map[string]interface{} {
	typeof := reflect.TypeOf(r)
	mapData := make(map[string]interface{})
	if typeof.Kind() == reflect.Struct {
		t := reflect.TypeOf(r)
		v := reflect.ValueOf(r)
		for i := 0; i < t.NumField(); i++ {
			if fileName := t.Field(i).Tag.Get("json"); fileName != "" {
				mapData[fileName] = v.Field(i).Interface()
			}
		}
	}
	return mapData
}

func (*BaseController) GetRedisTime() time.Duration {
	return time.Minute * time.Duration(config.GetInt64("REDIS_TIME", 3600)*10)
}

func (*BaseController) GetSecond(r int64) time.Duration {
	return time.Minute * time.Duration(r)
}
